package com.xinfu.newproject.ui;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.HomeActivity;
import com.xinfu.newproject.activity.LoginActivity;
import com.xinfu.newproject.activity.NoticeActivity;
import com.xinfu.newproject.activity.PersonActivity;
import com.xinfu.newproject.activity.ResidentCertificationActivity;
import com.xinfu.newproject.activity.SettingActivity;
import com.xinfu.newproject.adapter.my_provider.MyAdapter;
import com.xinfu.newproject.base.BaseFragment;
import com.xinfu.newproject.bean.item.ItemBean;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.bean.matter_repair.RepairDetailBean;
import com.xinfu.newproject.bean.matter_repair.RepairRecordBean;
import com.xinfu.newproject.bean.matter_repair.bean.RepairCountBean;
import com.xinfu.newproject.entity.NormalMultipleEntity;
import com.xinfu.newproject.presenter.RepairRecordPresenter;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.view.IView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

import static com.xinfu.newproject.contants.Constant.LOGIN;
import static com.xinfu.newproject.contants.Constant.USERIMAGE;
import static com.xinfu.newproject.contants.Constant.USERNAME;

public class MyFragment extends BaseFragment implements IView.IRepairRecordView {

    private List<NormalMultipleEntity> mData;

    @BindView(R.id.fragment_my_notice_iv)
    ImageView noticeIv;

    @BindView(R.id.fragment_my_rv)
    RecyclerView mRecyclerView;

    private MyAdapter myAdapter;
    private View headerView;

    private RepairRecordPresenter repairRecordPresenter = new RepairRecordPresenter(this);

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_my;
    }

    @Override
    protected void setUpView() {
        mRecyclerView.setLayoutManager(new GridLayoutManager(getMContext(), 4));
        initAdapter();
        headerView = getHeaderView(0, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isLogin()) {
                    ((HomeActivity) getMContext()).startIntentActivity(PersonActivity.class, null);
                } else {
                    ((HomeActivity) getMContext()).startIntentActivity(LoginActivity.class, null);
                }
            }
        });
        View footerView = getFooterView(1, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        refresh();
        myAdapter.addFooterView(footerView);
        myAdapter.addHeaderView(headerView);
        mRecyclerView.setAdapter(myAdapter);
    }

    private void initAdapter() {
        myAdapter = new MyAdapter();
        myAdapter.openLoadAnimation();
//        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
//            @Override
//            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Toast.makeText(HeaderAndFooterUseActivity.this, "" + Integer.toString(position), Toast.LENGTH_LONG).show();
//            }
//        });
        myAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        getData();
    }

    private void getData() {
        ItemBean itemBean1 = new ItemBean();
        itemBean1.setItemText("缴费记录");
        itemBean1.setItemImage(R.mipmap.payment_records_icon);
        itemBean1.setItemPosition(0);
        ItemBean itemBean2 = new ItemBean();
        itemBean2.setItemText("访客记录");
        itemBean2.setItemImage(R.mipmap.visitor_records_icon);
        itemBean2.setItemPosition(1);
        ItemBean itemBean3 = new ItemBean();
        itemBean3.setItemText("报修记录");
        itemBean3.setItemImage(R.mipmap.repair_record_icon);
        itemBean3.setItemPosition(2);
        ItemBean itemBean4 = new ItemBean();
        itemBean4.setItemText("投诉记录");
        itemBean4.setItemImage(R.mipmap.complaint_record_icon);
        itemBean4.setItemPosition(3);
        List<ItemBean> list = new ArrayList<>();
        list.add(itemBean1);
        list.add(itemBean2);
        list.add(itemBean3);
        list.add(itemBean4);
        myAdapter.setNewData(list);
    }

    private View getHeaderView(int type, View.OnClickListener listener) {
        View view = getLayoutInflater().inflate(R.layout.item_user_one, (ViewGroup) mRecyclerView.getParent(), false);
        if (type == 0) {
            ImageView settintIv = view.findViewById(R.id.item_user_one_set);
            settintIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((HomeActivity) getMContext()).startIntentActivity(SettingActivity.class, null);
                }
            });
        }
        view.setOnClickListener(listener);
        return view;
    }


    private View getFooterView(int type, View.OnClickListener listener) {
        View view = getLayoutInflater().inflate(R.layout.item_user_three, (ViewGroup) mRecyclerView.getParent(), false);
        if (type == 1) {

            RelativeLayout residentRL = view.findViewById(R.id.item_user_three_resident_rl);

            residentRL.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("type", -1);
                    ((HomeActivity) getMContext()).startIntentActivity(ResidentCertificationActivity.class, bundle);
                }
            });
        }
        view.setOnClickListener(listener);
        return view;
    }


    @Override
    protected void setUpData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        refresh();
        if (isLogin()) {
            repairRecordPresenter.repairCount();
        }
    }

    @OnClick({R.id.fragment_my_notice_iv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fragment_my_notice_iv:
                ((HomeActivity) getMContext()).startIntentActivity(NoticeActivity.class, null);
                break;
        }
    }

    private void refresh() {
//        Log.i("test")
        if (SPUtils.get(getMContext(), LOGIN, "0").equals("0")) {
            TextView nameTv = headerView.findViewById(R.id.item_user_one_name);
            ImageView nameIv = headerView.findViewById(R.id.item_user_one_iv);
            Glide.with(getMContext()).load(R.mipmap.user_normal_icon).asBitmap().into(nameIv);
            nameTv.setText("登录/注册");
        } else {
            String response = (String) SPUtils.get(getMContext(), LOGIN, "0");
            LoginBean.ResultBean resultBean = new Gson().fromJson(response, LoginBean.ResultBean.class);
            TextView nameTv = headerView.findViewById(R.id.item_user_one_name);
            ImageView nameIv = headerView.findViewById(R.id.item_user_one_iv);
            if (!SPUtils.get(getMContext(), USERNAME, "0").equals("0")) {
                nameTv.setText((String) SPUtils.get(getMContext(), USERNAME, "0"));
            } else {
                nameTv.setText(resultBean.getUserInfo().getUsername());
            }
            if (!SPUtils.get(getMContext(), USERIMAGE, "0").equals("0")) {
                Glide.with(getMContext()).load((String) SPUtils.get(getMContext(), USERIMAGE, "0")).asBitmap().error(R.mipmap.user_normal_icon).into(nameIv);
            } else {
                Glide.with(getMContext()).load(R.mipmap.user_normal_icon).asBitmap().into(nameIv);
            }

        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            refresh();
        }
    }

    /**
     * 判断是否可以请求数据
     */
    String token, userId, userName;

    private boolean isLogin() {

        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(getMContext(), LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
        } else {
            return false;
        }
        return true;
    }


    @Override
    public String header() {
        return token;
    }

    @Override
    public String userId() {
        return userId;
    }

    @Override
    public String repairId() {
        return null;
    }

    @Override
    public void showToast(String content) {

    }

    @Override
    public void closeDispose(Disposable disposable) {

    }

    @Override
    public void getData(RepairRecordBean repairRecordBean) {

    }

    @Override
    public void getData(RepairDetailBean repairDetailBean) {

    }

    @Override
    public void getData(RepairCountBean repairCountBean) {
        if (repairCountBean.getResult() != null && myAdapter != null) {
            myAdapter.addTextSize(repairCountBean.getResult());
        }
    }
}
